package com.lgywin.generate.comm;

import org.joou.UByte;

import java.io.UnsupportedEncodingException;

public class SecurityUtil {
    private static short[] xorScale = new short[] { 45, 66, 38, 55, 23, 254, 9, 165, 90, 19, 41, 45, 201, 58, 55, 37, 254, 185, 165, 169, 19, 171 };//.data文件的xor加解密因子
    /// <summary>
    /// 对数组进行异或
    /// </summary>
    /// <param name="buffer"></param>
    /// <returns></returns>
    public static byte[] Xor(byte[] buffer)
    {
        byte[] outBuffer = new byte[buffer.length];
        int iScaleLen = xorScale.length;
        for (int i = 0; i < buffer.length; i++)
        {
            short bufferShort = Short.parseShort(UByte.valueOf(buffer[i]).toString());
            outBuffer[i] = (byte)(bufferShort ^ xorScale[i % iScaleLen]);
        }
        return outBuffer;
    }
    public static void main(String[] args) throws UnsupportedEncodingException {
        String str = "中文";
        byte[] bs = Xor(str.getBytes("GBK"));
        for (int i = 0; i < bs.length; i++) {
            System.out.print(UByte.valueOf(bs[i]).toString()+" "+bs[i]+"===");
        }
        bs = Xor(bs);
        System.out.println("========");
        System.out.println(new String(bs,"GBK"));
    }
}
